{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f9f395a3",
   "metadata": {},
   "source": [
    "上机 2-01:\n",
    "\n",
    "找一段英文的文本，统计该文本中单词的出现次数。比如：How are you.How are you 统计结果是：{\"how\":2,\"are\":2,\"you\":2}\n",
    "\n",
    "(推荐使用的文本：https://github.com/qiwsir/StarterLearningPython/blob/master/newcodes/answers/youraisemeup.txt)\n",
    "\n",
    "根据上述要求，编写一段程序。\n",
    "\n",
    "提示：用下述方式逐行获取 txt 文件中的文本\n",
    "\n",
    "```python\n",
    "with open(\"youraisemeup.txt\") as f:\n",
    "    for line in f:\n",
    "    if bool(line):\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cab35994",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'you': 5, 'raise': 4, 'me': 5, 'up': 4, 'when': 3, 'i': 6, 'am': 4, 'down': 1, 'and': 4, 'oh': 1, 'my': 2, 'soul': 1, 'so': 2, 'weary': 1, 'troubles': 1, 'come': 2, 'heart': 1, 'burdened': 1, 'be': 2, 'then': 1, 'still': 1, 'wait': 1, 'here': 1, 'in': 1, 'the': 1, 'silenceuntil': 1, 'sit': 1, 'awhile': 1, 'with': 1, 'can': 2, 'stand': 1, 'on': 3, 'mountains': 1, 'to': 2, 'walk': 1, 'stormy': 1, 'seas': 1, 'strong': 1, 'your': 1, 'shoulders': 1, 'more': 1, 'than': 1}\n"
     ]
    }
   ],
   "source": [
    "with open('youraisemeup.txt', 'r') as f:\n",
    "\n",
    "    word_count = {}\n",
    "    \n",
    "    for line in f:\n",
    "        if bool(line.strip()):\n",
    "            line = line.lower()\n",
    "            words = line.split()\n",
    "            \n",
    "            for word in words:\n",
    "                if word in word_count:\n",
    "                    word_count[word] += 1\n",
    "                else:\n",
    "                    word_count[word] = 1\n",
    "\n",
    "print(word_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7fa5d59",
   "metadata": {},
   "source": [
    "上机2-02：\n",
    "\n",
    "已知：字符串a = \"aAsmr3idd4bgs7Dlsf9eAF\"，\n",
    "\n",
    "要求：编写程序，完成如下任务：\n",
    "\n",
    "请将字符串中的数字取出，并输出成一个新的字符串。\n",
    "\n",
    "请统计字符串出现的每个字符的出现次数（忽略大小写，a与A是同一个字母），并输出成一个字典。 例 {'a':3,'b':1}\n",
    "\n",
    "请去除字符串多次出现的字母，不区分大小写。例 'aAsmr3idd4bgs7Dlsf9eAF'，经过去除后，输出       'asmr3id4bg7lf9e‘\n",
    "\n",
    "\n",
    "\n",
    "提示：\n",
    "\n",
    "判断是否数字/字母： isdigit() /  isalpha()\n",
    "\n",
    "若要保持原顺序，c.sort(key=a.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3e5e75d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数字字符串: 3479\n",
      "字符出现次数: {'a': 3, 's': 3, 'm': 1, 'r': 1, 'i': 1, 'd': 3, 'b': 1, 'g': 1, 'l': 1, 'f': 2, 'e': 1}\n",
      "去除重复字符后的字符串: asmr3id4bg7lf9e\n"
     ]
    }
   ],
   "source": [
    "a = \"aAsmr3idd4bgs7Dlsf9eAF\"\n",
    "a_lower = a.lower()\n",
    "digits_str = \"\"  # 数字字符串\n",
    "char_count = {}  # 字符出现次数\n",
    "\n",
    "for char in a_lower:\n",
    "    if char.isdigit():\n",
    "        digits_str += char\n",
    "    elif char in char_count:\n",
    "        char_count[char] += 1\n",
    "    else:\n",
    "        char_count[char] = 1\n",
    "\n",
    "unique_set = set(a_lower)  # remove duplicates\n",
    "unique_list = list(unique_set)  # convert to list to use sort()\n",
    "unique_list.sort(key=a_lower.index)  # sorting\n",
    "unique_str = \"\".join(unique_list)  # join to string\n",
    "\n",
    "print(f\"数字字符串: {digits_str}\")\n",
    "print(f\"字符出现次数: {char_count}\")\n",
    "print(f\"去除重复字符后的字符串: {unique_str}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
